import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login/index.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    component: () => import('../views/layout/index.vue'),
    children: [
      {
        path: '/',
        component: () => import('../views/Home/index.vue')
      },
      {
        path: 'artCategory',
        component: () => import('../views/art/artCategory.vue')
      },
      {
        path: 'artList',
        component: () => import('../views/art/artList.vue')
      },
      {
        path: 'userInfo',
        component: () => import('../views/user/userInfo.vue')
      },
      {
        path: 'avatar',
        component: () => import('../views/user/avatar.vue')
      },
      {
        path: 'rePwd',
        component: () => import('../views/user/rePwd.vue')
      }
    ]
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/reg',
    component: () => import('../views/reg/index.vue')
  },
  {
    path: '*',
    component: () => import('../views/noFound.vue')
  }
]

const router = new VueRouter({
  routes
})
// 路由前置导航守卫
// 白名单列表
const whiteList = ['/login', '/reg']
router.beforeEach((to, from, next) => {
  // 先判断是否访问的是有权限的页面
  if (whiteList.includes(to.path)) {
    // 用户访问的是登录或者注册页面 不需要拦截 直接放行
    next()
  } else {
    // 进入else分支说明用户访问的是有权限的页面, 先判断是否有token
    // 如果有token直接放行
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      // 没有token拦截到登录页面让用户重新登录
      next('/login')
    }
  }
})
export default router
